/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._500;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 给定一个单词列表，只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
 *
 * 输入: ["Hello", "Alaska", "Dad", "Peace"]
 * 输出: ["Alaska", "Dad"]
 *
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._500
 * @ClassName: KeyboardRow
 * @Author: linyaonan
 * @Date: 2019/12/31 15:46
 */
public class KeyboardRow {

    public String[] findWords(String[] words) {

        int[] z = new int[26];
        z['q'-'a'] = 0;
        z['w'-'a'] = 0;
        z['e'-'a'] = 0;
        z['r'-'a'] = 0;
        z['t'-'a'] = 0;
        z['y'-'a'] = 0;
        z['u'-'a'] = 0;
        z['i'-'a'] = 0;
        z['o'-'a'] = 0;
        z['p'-'a'] = 0;

        z[0] = 1;
        z['s'-'a'] = 1;
        z['d'-'a'] = 1;
        z['f'-'a'] = 1;
        z['g'-'a'] = 1;
        z['h'-'a'] = 1;
        z['j'-'a'] = 1;
        z['k'-'a'] = 1;
        z['l'-'a'] = 1;

        z['z'-'a'] = 2;
        z['x'-'a'] = 2;
        z['c'-'a'] = 2;
        z['v'-'a'] = 2;
        z['b'-'a'] = 2;
        z['n'-'a'] = 2;
        z['m'-'a'] = 2;

        List<String> list = new ArrayList<>();

        for (String word : words) {
            String lWord = word.toLowerCase();
            int first = z[lWord.charAt(0) - 'a'];
            boolean pick = true;
            for (char c : lWord.toCharArray()) {
                if (z[c - 'a'] != first) {
                    pick = false;
                    break;
                }
            }
            if (pick) {
                list.add(word);
            }
        }

        String[] r = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            r[i] = list.get(i);
        }

        return r;
    }

}
